package com.tongji.travelplan.dao;

//import java.io.InputStream;
import java.sql.*;
//import java.util.Properties;



public class DBUtil 
{
	private static Connection ct = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	
	//declare the parameters to connect mysql
	private static String url = "jdbc:mysql://localhost:3306/travelplan";
	private static String username = "root";
	private static String driver = "com.mysql.jdbc.Driver";
	private static String password = "root";
	
	private static CallableStatement cs = null;
	public static CallableStatement getCs()
	{
		return cs;
	}
	
	//private static Properties pp = null;
	//private static InputStream fis = null;
	static
	{
		try {
			Class.forName(driver).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	//get the connection
	public static Connection getConnection()
	{
		try
		{
			ct = DriverManager.getConnection(url,username,password);
		}catch (Exception e) {e.printStackTrace();
			// TODO: handle exception
		}
		return ct;
	}
	
	
	public static ResultSet executeQuery(String sql, String[] parameters)
	{
		try{
			ct = getConnection();
			ps = ct.prepareStatement(sql);
			if(parameters!=null)
			{
				for(int i = 0; i < parameters.length; i++)
				{
					ps.setString(i+1, parameters[i]);
				}
			}
			rs = ps.executeQuery();
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			throw new RuntimeException(e.getMessage());
		}
		finally{
			
		}
		return rs;
	}
	
	public static Connection getCt()
	{
		return ct;
	}
	public static PreparedStatement getPs()
	{
		return ps;
	}
	public static ResultSet getRs()
	{
		return rs;
	}
	
	public static void executeUpdates(String sql, String[] parameters)
	{
		try{
			ct = getConnection();
			ps = ct.prepareStatement(sql);
			if(parameters != null)
			{
				for(int i = 0 ; i < parameters.length ; i++)
				{
					ps.setString(i+1, parameters[i]);
				}
			}
			ps.executeUpdate();
		}catch (Exception e) {
			throw new RuntimeException(e.getMessage());
			//e.printStackTrace();
			// TODO: handle exception
		}finally{
			close(rs,ps,ct);
		}
	}
	public static void close(ResultSet rs , PreparedStatement ps , Connection ct)
	{
		//close the resource
		if(rs!=null)
		{
			try{
				rs.close();
			}catch (Exception e) {
				System.out.println(e);
				// TODO: handle exception
			}
			rs = null;
		}
	
		if(ps!=null)
		{
			try{
				ps.close();
			}catch (Exception e) {
				System.out.println(e);
				// TODO: handle exception
			}
			ps = null;
		}
		
		if(ct!=null)
		{
			try{
				ct.close();
			}catch (Exception e) {
				System.out.println(e);
				// TODO: handle exception
			}
		ct = null;
		}
	}
}
